package view.chart;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class TesteChart extends JPanel{
	private static final long serialVersionUID = 1L;

	public static void main(String[] args){  
        Random rand = new Random();  
          
        String[] categorias = new String[]{"Categoria A", "Categoria B", "Categoria C"};  
          
        TimeSeriesCollection dataset = new TimeSeriesCollection();
          
        Calendar c = Calendar.getInstance();
        c.clear();
        for(int i=0; i<categorias.length; i++){        	
            TimeSeries series = new TimeSeries(categorias[i]);  
            for(int j=0; j<5; j++){  
                c.set(Calendar.HOUR_OF_DAY, j);  
                series.add(new Hour(c.getTime()), 0 + rand.nextInt(2 + (i * j * 2)) + j * j);  
            }  
            dataset.addSeries(series);  
        }
        
        ValueAxis axis1 = new NumberAxis("Eixo X");  
        NumberAxis axis2 = new NumberAxis("Eixo Y");  
          
        StandardXYItemRenderer renderer = new StandardXYItemRenderer();  
          
        XYPlot plot = new XYPlot(dataset, axis1, axis2, renderer);  
          
        axis2 = (NumberAxis) plot.getRangeAxis();  
        DecimalFormatSymbols simboloSeparacion = new DecimalFormatSymbols();  
        simboloSeparacion.setDecimalSeparator('.');  
          
        axis2.setLowerBound(0);  
        axis2.setUpperBound(70);  
          
        double minutos = axis2.getUpperBound();  
        String hh = "";  
        String mm = "";  
          
        String hhmm = "" + minutos / 60;  
                 
        int x = hhmm.indexOf(".",0);  
          
        for(int a=0; a<x; a++){  
            hh = hh + hhmm.charAt(a);  
        }
        
        for(int a=x; a<3; a++){  
            mm = mm + hhmm.charAt(a + 1);
        }
        
        DecimalFormat formatter = new DecimalFormat(hh + ":" + mm + "h");  
  
        axis2.setTickUnit(new NumberTickUnit(5, formatter));
        
        axis2.setTickLabelPaint(Color.gray);
        
        JFreeChart chart = new JFreeChart(plot);  
          
        ChartPanel panel = new ChartPanel(chart);  
        
        JFrame f = new JFrame("teste");  
        f.setSize(640, 480);  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        f.getContentPane().add(panel);
        f.pack();
        f.setVisible(true);  
    }
}